<?php 

// Version
define('VERSION', '1.5.1');

// Config
require_once('config.php');

require_once(DIR_ASYN.'asynstartup.php');

require_once(DIR_SYSTEM.'library/asyntask.php');

// Registry
$registry = new Registry();

// Loader
$loader = new Loader($registry);
$registry->set('load', $loader);

// Database 
$db = new DB(DB_DRIVER, DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
$registry->set('db', $db);

// Log 
$log = new Log('ASYNLOG_FILE.txt');
$registry->set('log', $log);


//处理错误，把错误输出到文件日志中.
function error_handler($errno, $errstr, $errfile, $errline) {
	global $log, $config;
	
	switch ($errno) {
		case E_NOTICE:
		case E_USER_NOTICE:
			$error = 'Notice';
			break;
		case E_WARNING:
		case E_USER_WARNING:
			$error = 'Warning';
			break;
		case E_ERROR:
		case E_USER_ERROR:
			$error = 'Fatal Error';
			break;
		default:
			$error = 'Unknown';
			break;
	}
		
	$log->write('PHP ' . $error . ':  ' . $errstr . ' in ' . $errfile . ' on line ' . $errline);
	

	return true;
}
	
// Error Handler
set_error_handler('error_handler');

// Request
$request = new Request();
$registry->set('request', $request);

// Response
$response = new Response($log);
$registry->set('response', $response); 


// Session
$session = new Session();
$registry->set('session', $session); 

//weiboclient
if(isset($request->get['oauth_token'])){
$weiboclient=new WeiboClient(WB_AKEY,WB_SKEY,$request->get['oauth_token'],$request->get['oauth_token_secret']);
$registry->set('weiboclient',$weiboclient);
}

	
// Front Controller 
$controller = new Front($registry);

// Router
if (isset($request->get['route'])) {
	$action = new Action($request->get['route']);
} 

// Dispatch
$controller->dispatch($action, new Action('error/notfound'));

//out put to the log.
$response->output();
?>